<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <script src="http://localhost:8080/auth/js/keycloak.js"></script>
    <script>
    	var keycloak = new Keycloak({
        	url: 'http://localhost:8080/auth',
        	realm: 'smartpiggy',
        	clientId: 'smartpiggy'    	
    	});
        
    	var initOptions={
    		onLoad: 'login-required',
    		checkLoginIframe: false,
    		responseMode:'fragment', 
    		flow: 'implicit',
    		redirectUri: 'http://localhost/index'
    		
    	};
    	keycloak.init(initOptions).success(function(authenticated) {
 		    var username=keycloak.tokenParsed.preferred_username;
 			var url = 'http://client.cy9.petrochina/login?username='+username;

    	    var req = new XMLHttpRequest();
    	    req.open('get', url, true);
    	    //req.setRequestHeader('Accept', 'application/json');
    	    //req.setRequestHeader('Authorization', 'Bearer ' + keycloak.Token);
    	    req.setRequestHeader("Content-Type","application/x-www-form-urlencoded;")
			 req.onreadystatechange = function () {
    	        if (req.readyState == 4) {
    	            if (req.status == 200) {
    	                //alert('Success');
    	                window.document.location.href='http://localhost/index';
    	            } else if (req.status == 403) {
    	                alert('Forbidden');
    	            }
    	        }
    	    } 

    	    req.send();
    	    
        }).error(function() {
            alert('failed to initialize:');
        });
    	
    
    </script>
</head>
<body>
   
   <!--  <a href="/" onclick="keycloak.logout('http://localhost')">lgout</a>
-->
</body>
</html>
